/***************************************************************************
 $ @Description: 
 $ @Author: NavyPeng
 $ @Date: 2024-04-08 14:23:18
 $ @LastEditTime: 2024-04-19 15:13:01
 $ @LastEditors: NavyPeng
 ***************************************************************************/
#include "Ctrl.h"

#if PrototypePattern2_

#include <map>
#include <unordered_map>

//1. 抽象原型类
class Shape
{
public:
	virtual ~Shape() {}
	virtual Shape* clone() = 0;
	virtual int getid() = 0;
	virtual string getType() = 0;
protected:
	string Type;
private:
	int id;
};

//2. 三个形状具体原型
class Circle :public Shape
{
public:
	Circle(string Type, int id) :Type(Type), id(id) {}
	~Circle() {}
	//Circle(const Circle& lhs) { Type = lhs.Type, id = lhs.id; }
	Shape* clone() { return new Circle{ *this }; }
	int getid() { return id; }
	string getType() { return Type; }
protected:
	string Type;
private:
	int id;
};

class Rectangle :public Shape
{
public:
	Rectangle(string Type, int id) :Type(Type), id(id) {}
	~Rectangle() {}
	Rectangle(const Rectangle& lhs) { Type = lhs.Type, id = lhs.id; }
	Shape* clone() { return new Rectangle{ *this }; }
	int getid() { return id; }
	string getType() { return Type; }
protected:
	string Type;
private:
	int id;
};

class Square :public Shape
{
public:
	Square(string Type, int id) :Type(Type), id(id) {}
	~Square() {}
	Square(const Square& lhs) { Type = lhs.Type, id = lhs.id; }
	Shape* clone() { return new Square{ *this }; }
	int getid() { return id; }
	string getType() { return Type; }
protected:
	string Type;
private:
	int id;
};

//3. 存储对象种类的数据库
class ShapeType
{
public:
	~ShapeType()
	{
		for (auto& x : ShapeMap)
		{
			delete x.second;
			x.second = nullptr;
		}
	}
	//构造原始对象
	ShapeType()
	{
		Circle* circle = new Circle{ "圆形",1 };
		Square* square = new Square{ "正方形",2 };
		Rectangle* rectangle = new Rectangle{ "矩形",3 };
		ShapeMap.emplace(circle->getType(), circle);
		ShapeMap.emplace(square->getType(), square);
		ShapeMap.emplace(rectangle->getType(), rectangle);
	}
	//根据你所需要的种类来获得克隆对象
	Shape* getShape(string Type)
	{
		return ShapeMap[Type]->clone();
	}
private:
	unordered_map<string, Shape*> ShapeMap;
};

int main()
{
	//1. 创建对象种类库
	ShapeType obj;

	//2. 从对象库中获得对象的克隆体（getShape函数返回某个对象的克隆）
	Shape* m_circle = obj.getShape("圆形");
	Shape* m_Square = obj.getShape("正方形");
	Shape* m_Rectangle = obj.getShape("矩形");



	cout << m_circle->getid() << " : " << m_circle->getType() << endl;
	cout << m_Square->getid() << " : " << m_Square->getType() << endl;
	cout << m_Rectangle->getid() << " : " << m_Rectangle->getType() << endl;


	delete m_circle;
	delete m_Square;
	delete m_Rectangle;
	return 0;
}

#endif